<?php 
/*******************************************************************************\
* 生成缩略图，水印函数                                                          *
*                                                                               *
* filename          => 文件名(带路径)                                           *
* area              => 缩略图宽高                                               *
* watername         => 文件名 / 水印文字                                        *
* font              => 字体                                                     *
* fontsize          => 字体大小                                                 *
* fontcolor         => 字体颜色                                                 *
* angle             => 字体倾斜度                                               *
* status            => 是否使用水印     1,使用  2,不使用                        *
* watertype         => 水印类型         1,文字  2,图片                          *
* outset            => 水印位置         1,左上  2,右上  3,左下  4,右下          *
* startX            => 水印X位置                                                *
* startY            => 水印Y位置                                                *
* path              => 图片保存位置                                             *
\*******************************************************************************/
function gd ($data) {
    $data['outset']       = !empty($data['outset']) ? $data['outset'] : 4;
    $data['startX']       = !empty($data['startX']) ? $data['startX'] : 0;
    $data['startY']       = !empty($data['startY']) ? $data['startY'] : 0;
    $data['fontsize']     = !empty($data['fontsize']) ? $data['fontsize'] : 14;
    $data['angle']        = !empty($data['angle']) ? $data['angle'] : 0;
    $data['pict']         = !empty($data['pict']) ? $data['pict'] : 60;
    $data['font']         = empty($data['font']) ? 'C:/windows/fonts/STXINGKA.TTF' : '../fonts/' . $data['font'];

    // 颜色转换
    $fontcolor            = !empty($data['fontcolor']) ? $data['fontcolor'] : 0;
    $sixteen = str_replace('#', '', $fontcolor);
    $first   = hexdec(substr($sixteen, 0, 2));
    $second  = hexdec(substr($sixteen, 2, 2));
    $third   = hexdec(substr($sixteen, 4, 2));

    $file       = explode("/", $data['filename']);
    
    $path_parts = pathinfo(strtolower($data['filename']));
    $files      = $path_parts['basename'];
	$uploadExt  = $path_parts['extension'];

    if ($uploadExt == 'jpg' || $uploadExt == 'jpeg') {
        # 从 JPEG 文件或 URL 新建 原始图片
        $src  = imagecreatefromjpeg($data['filename']);
        $src1 = imagecreatefromjpeg($data['filename']);
    } elseif ($uploadExt == 'gif') {
        # 从 GIF 文件或 URL 新建 原始图片
        $src  = imagecreatefromgif($data['filename']);
        $src1 = imagecreatefromgif($data['filename']);
    } elseif ($uploadExt == 'png') {
        # 从 PNG 文件或 URL 新建 原始图片
        $src  = imagecreatefrompng($data['filename']);
        $src1 = imagecreatefrompng($data['filename']);
    } elseif ($uploadExt == 'bmp') {
        # 从 BMP 文件或 URL 新建 原始图片
        $src  = imagecreatefromwbmp($data['filename']);
        $src1 = imagecreatefromwbmp($data['filename']);
    } else {
        return;
    }

    # 图片原始宽高
    $width  = ImageSx($src);
    $height = ImageSy($src);

    # 使用水印
    if ($data['status'] == 1) {
        # 水印为图片
        if ($data['watertype'] == 2) {
            $wpath_parts = pathinfo(strtolower($data['waterpic']));
            $wfiles      = $wpath_parts['basename'];
            $wuploadExt  = $wpath_parts['extension'];

            if ($wuploadExt == 'gif') {
            # 从 GIF 文件新建水印
                $s = imagecreatefromgif($data['waterpic']);
            } elseif ($wuploadExt == 'png') {
            # 从 png 文件新建水印
                $s = imagecreatefrompng($data['waterpic']);
            } else {
            # 从 其他 文件新建水印
                $s = imagecreatefromjpeg($data['waterpic']);
            }

            $waterwidth  = ImageSx($s);
            $waterheight = ImageSy($s);

            // 水印位置
            Switch ($data['outset']) {
                Case 1:
                    $wstartX = $data['startX'];
                    $wstartY = $data['startY'];
                    break;
                Case 2:
                    $wstartX = $width - $waterwidth - $data['startX'] ;
                    $wstartY = $data['startY'];
                    break;
                Case 3:
                    $wstartX = $data['startX'];
                    $wstartY = $height - $waterheight - $data['startY'];
                    break;
                Case 4:
                    $wstartX = $width - $waterwidth - $data['startX'] ;
                    $wstartY = $height - $waterheight - $data['startY'];
                    break;
                Case 5:
                    $wstartX = ($width - $waterwidth) / 2;
                    $wstartY = ($height - $waterheight) / 2;
                    break;
            }

            # 给原始图片打上水印
            # 3,4缩略图从左上角开始的位置
            # 5,6缩略图从右下角开始的位置
            # 7,8缩略图宽高
            # 9,透明度
            
            if ($wuploadExt == 'png') {
                $dst_photo = imagecreatetruecolor($width, $height);            
                imageCopy($src, $s, $wstartX, $wstartY, 0, 0, $waterwidth, $waterheight);
            } else {
                imagecopymerge($src, $s, $wstartX, $wstartY, 0, 0, $waterwidth, $waterheight, $data['pict']);
            }
            # 生成带水印的原图
            $wfilename = $data['path'] . '/w_' . $files;
            imagejpeg($src, $wfilename);
        }
        # 水印为文字
        else {
            // 内容转换为UTF-8
            //$text = mb_convert_encoding($data['watername'], "UTF-8", "GB2312");
            $text = $data['watername'];

            $data['fontcolor']    = ImageColorAllocate($src, $first,$second,$third);
            
            // 从字体库读取字体
            $font = $data['font'];
            $fontsize = empty($data['fontsize']) ? 14 : $data['fontsize'];
            $angle = empty($data['angle']) ? 0 : $data['angle'];
            $color = empty($data['fontcolor']) ? 0 : $data['fontcolor'];

            $lens = strlen($text);
            $fsize   = imagettfbbox($fontsize, 0, $font, $text);
            $fontwidth  = $fsize[2] - $fsize[0];
            $fontheight = $fsize[1] - $fsize[7];
            //$fontwidth = $lens * $fontsize / 2;

            Switch ($data['outset']) {
                Case 1:
                    $wstartX = 0;
                    $wstartY = $fontheight;
                    break;
                Case 2:
                    $wstartX = 0;
                    $wstartY = $height - $fontheight;
                    break;
                Case 3:
                    $wstartX = $width - $fontwidth;
                    $wstartY = $fontheight;
                    break;
                Case 4:
                    $wstartX = $width - $fontwidth;
                    $wstartY = $height - $fontheight;
                    break;
                Case 5:
                    $wstartX = ($width - $fontwidth) / 2;
                    $wstartY = ($height - $fontheight) / 2;
                    break;
            }

            # 1     => 源图片
            # 2     => 字体大小
            # 3     => 旋转角度
            # 4,5   => 左上角起的坐标位置
            # 6     => 字体颜色
            # 7     => 字体
            # 8     => 内容
            imagettftext($src, $fontsize, $angle, $wstartX, $wstartY, $color, $font, $text);
            $wfilename = $data['path'] . '/w_' . $files;
            imagejpeg($src, $wfilename);
        }
    }

# 压缩图片
    foreach ($data['area'] AS $k => $v) {
    # 生成有水印的压缩图
        # 有水印的原图
        $wpath_parts1 = pathinfo(strtolower($wfilename));
        $wfiles1      = $wpath_parts1['basename'];
        $wuploadExt1  = $wpath_parts1['extension'];

        if ($wuploadExt1 == 'gif') {
        # 从 GIF 文件新建水印
            $wsrc = imagecreatefromgif($wfilename);
        } elseif ($wuploadExt1 == 'png') {
        # 从 png 文件新建水印
            $wsrc = imagecreatefrompng($wfilename);
        } else {
        # 从 其他 文件新建水印
            $wsrc = ImageCreateFromJPEG($wfilename);
        }
        
        // 新建一个缩略图的真彩色图像
        $dst  = ImageCreateTrueColor($v['x'],$v['y']);

        $bgcolor = imagecolorallocate($dst,255,255,255);
        imagefill($dst,0,0,$bgcolor);


        # 缩略片等比缩放后的宽高
        $getXY = getPicXY($data['filename'], $v['x'], $v['y']);
        $newWidth  = $getXY[0];
        $newHeight = $getXY[1];


        # 如果缩放后大小大于原始大小，图片不变
        if ($newWidth > $width) {
            $newWidth = $width;
        } else {
            $newWidth = $newWidth;
        }
        if ($newHeight > $height) {
            $newHeight = $height;
        } else {
            $newHeight = $newHeight;
        }

        # 获得缩略图从左上角的起点位置
        if ($newWidth == $v['x']) {
            $startX = '0';
            $startY = ($v['y'] - $newHeight) / 2;
        } elseif ($newHeight == $v['y']) {
            $startX = ($v['x'] - $newWidth) / 2;
            $startY = '0';
        } elseif ($newWidth > ($v['x']) && ($newHeight > $v['y'])) {
            $startX = ($newWidth - $v['x']) / 2;
            $startY = ($newHeight - $v['y']) / 2;
        } elseif ($newWidth < ($v['x']) && ($newHeight < $v['y'])) {
            $startX = ($v['x'] - $newWidth) / 2;
            $startY = ($v['y'] - $newHeight) / 2;
        }

        # 3,4缩略图从左上角开始的位置
        # 5,6缩略图从右下角开始的位置
        # 7,8缩略图等比缩放后的宽高
        # 9,10原始图片宽高
        if ($data['status'] == 1) {
            ImageCopyResampled($dst, $wsrc, $startX, $startY, 0, 0, $newWidth, $newHeight, $width, $height); 

            $swfilename = $data['path'] . '/sw[' . $k . ']_' . $files;
            imagejpeg($dst, $swfilename);
        }
    # 生成无水印的压缩图
        # 从 JPEG 文件或 URL 新建 原始图片
        //$src = ImageCreateFromJPEG($data['filename']);

        ImageCopyResampled($dst, $src1, $startX, $startY, 0, 0, $newWidth, $newHeight, $width, $height); 

        $sfilename = $data['path'] . '/s[' . $k . ']_' . $files;
        imagejpeg($dst, $sfilename);
    }
    
    # 清除缓存
    unset($wfilename);
    unset($swfilename);
    unset($sfilename);
    unset($data);
}
?>